import { Request, Response, NextFunction } from 'express';
import _ from 'lodash';
import { getComplaint, getIdComplaint, createComplaint, updateComplaint, deleteComplaint, createFile, findFileById } from './complaint.service';
import { IMAGE_TYPE_COMPLAINT } from '../app/constants';
import { getFavorite } from '../favorites/favorites.service';

/**
 * 获取内容
 */
export const index = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  const data = await getComplaint();
  response.send(data);
}

/**
 * 获取内容(按 ID 查找)
 */
export const complaintDetail = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取内容 ID
  const { postId } = request.params;

  // 调取内容
  try {
    const data = await getIdComplaint(parseInt(postId, 10));

    let images = await findFileById(0, data.id, IMAGE_TYPE_COMPLAINT);

    let favoriteParam = { user_id: data.user_id, type: 3, target_id: data.id }
    let favorite = await getFavorite(favoriteParam);

    data.images = images;
    data.favorite = (favorite as any).length > 0 ? favorite[0] : null;

    // 做出响应
    response.send(data);
  } catch (error) {
    next(error);
  }
};

/**
 * 创建内容
 */
export const store = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取内容
  const complaintModel = request.body;
  complaintModel.status = 0;

  // 创建
  try {
    const data = await createComplaint(complaintModel);
    response.send(data);
  } catch (error) {
    next(error);
  }
}

/**
 * 更新内容
 */
export const update = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取更新内容 ID
  const { postId } = request.params;

  // 获取更新内容
  const post = _.pick(request.body, ['title', 'content']);

  // 更新
  try {
    const data = await updateComplaint(post, parseInt(postId, 10));
    response.send(data);
  } catch (error) {
    next(error);
  }
}

/**
 * 删除内容
 */
export const destroy = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取更新内容 ID
  const { postId } = request.params;

  // 删除
  try {
    const data = await deleteComplaint(parseInt(postId, 10));
    response.send(data);
  } catch (error) {
    next(error);
  }
}

/**
 * 上传文件
 */
export const uploads = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取类型
  let { target_type, target_id } = request.body;
  if (target_type == undefined) {
    target_type = request.query.target_type;
    target_id = request.query.target_id;
  }

  // 文件信息
  const fileInfo = _.pick(request.file, [
    'target_id',
    'target_type',
    'url',
    'originalname',
    'mimetype',
    'filename',
    'size'
  ])

  fileInfo['url'] = "/complaint/";
  fileInfo['target_type'] = target_type;
  fileInfo['target_id'] = target_id;

  try {
    // 保存文件信息
    const data = await createFile({
      ...fileInfo,
      //target_type: parseInt(`${target_type}`, 10), // target_type: `${target_type}` // target_type: target_type as string  转换字符串两种写法
    })
    let post = {
      id: parseInt(`${target_id}`, 10),
      image_id: (data as any).insertId,
    }

    const complaintData = await updateComplaint(post as any, parseInt(`${target_id}`, 10));

    // 做出响应
    response.status(200).send(complaintData)
  } catch (error) {
    next(error);
  }
}

/**
 * 文件服务
 */
export const serve = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 从地址参数里得到文件 ID
  const { fileId } = request.params;

  try {
    // 查找文件信息
    const file = await findFileById(parseInt(fileId, 10), 0, 0);

    // 做出响应
    response.sendFile(file.filename, {
      root: 'uploads/complaint',
      headers: {
        'Content-Type': file.mimetype
      }
    });
  } catch (error) {
    next(error);
  }
}
